#ifndef SOCKET_REACTOR_H
#define SOCKET_REACTOR_H

#include "Connection.hpp"
#include <sys/epoll.h>

namespace DBServer
{
namespace Socket
{

class Reactor {
public:

    // create a reactor
    Reactor();
    ~Reactor();

    // EventLoop
    virtual int run();     // epoll_wait, moniter the socket in the rb tree
    virtual int stop();
    virtual void add_conn(std::shared_ptr<ServerConn>);

    virtual int register_read(Connection*);
    virtual int register_write(Connection*);
    virtual int register_rdhup(Connection* conn);
    virtual int delete_event(Connection*);

    virtual int get_epoll_fd();
    virtual int get_socket_num();

    virtual void add_eve_write(Connection &conn);
    virtual void remote_eve_write(Connection &conn);

    // can't allow to construct by copy
    Reactor(const Reactor&&) = delete;
    Reactor(const Reactor&) = delete;
    const Reactor& operator=(const Reactor&) = delete;
    const Reactor&& operator=(const Reactor&&) = delete;
private:
    int reactor_init();

private:
    int is_stop;
    int status;
    int epoll_fd;
    unsigned long monitor_fd;       // the number of sockets in this epoll
    std::unordered_map<int, std::shared_ptr<ServerConn>> map;
};

} // namespace Socket
} // namespace DBServer


#endif